<template>
    <rx-dialog @handOk="onOk" @cancel="onCancel">
        <a-form :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
            <a-form-item :label="i18n('formReport.report','列表字段')">
                <rx-input-button v-model="reportVal"
                                 @click="selectReportView"
                                 style='width: 300px;'
                >
                </rx-input-button>
            </a-form-item>
            <a-form-item :label="i18n('formReport.paramsConf','参数配置')">
                <a-table :dataSource="localConfig.params" :pagination="false"
                         rowKey="idx_">
                    <a-table-column :title="i18n('formReport.reportSelParam','报表查询参数')" dataIndex="bindFieldLabel" width="150px">
                        <template slot-scope="text, record">
                            <a-form-item class="tableItems">
                                {{ record.name }}
                            </a-form-item>
                        </template>
                    </a-table-column>
                    <a-table-column :title="i18n('formReport.listField','列表字段')" dataIndex="returnField" width="200px">
                        <template slot-scope="text, record">
                            <a-select @change="onSelectChange"
                                v-model="record.srcField" :options="headerFields"/>
                        </template>
                    </a-table-column>
                </a-table>
            </a-form-item>
        </a-form>
    </rx-dialog>
</template>

<script>
    import FormBoListApi from "@/api/form/core/formBoList";
    import {BaseList, Util} from "@lowcode/jpaas-common-lib";
    import FormReportDialog from "./FormReportDialog";

/**
 * 数据结构:
 * {
 *     //报表id
 *     id: "",
 *     //报表名称
 *     name: "",
 *     //报表查询参数配置
 *     params:[ {name:"参数名",type:"数据类型",defaultValue:"默认值",srcField:"来源字段"}]
 * }
 */

export default {
    name: 'FormReportSetting',
    mixins: [BaseList],
    components: {
    },
    props:["pkId","config", "layerid","destroy"],
    data() {
        return {
            headerFields:[],
            selectRowKeys:[],
            reportVal: "",
            //配置信息
            localConfig: {
                id: "",//报表id
                name: "",//报表名称
                params:[]
            }
        }
    },
    created() {
        if(this.config){
            this.localConfig=this.config;
            this.reportVal = JSON.stringify({text: this.config.name, value: this.config.id});
        }

        //加载列头信息
        this.reloadColumns();
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formBoList",key);
        },
        reloadColumns() {
            let self_ = this;
            FormBoListApi.reloadColumns(this.pkId).then(res => {
                self_.headerFields = res.data.map(function (item) {
                    item.value = item.field;
                    item.label=item.header;
                    return item
                });
            });
        },
        selectReportView(){
            var self_=this;
            var config = {
                curVm: this,
                widthHeight: ['800px', '600px'],
                component: FormReportDialog,
                title: self_.i18n('formReport.pdfSel','PDF模板选择'),
            };
            Util.open(config, function (action,data) {
                if (action != 'ok') return;
                self_.reportVal = JSON.stringify({text: data.name, value: data.id});
                self_.localConfig.id=data.id;
                self_.localConfig.name=data.name;
                self_.localConfig.params=[];
                if(data.ureportFileQueryParameterList){
                    for (let i = 0; i < data.ureportFileQueryParameterList.length; i++) {
                        let obj=data.ureportFileQueryParameterList[i];
                        self_.localConfig.params.push({
                            name:obj.name,
                            type:obj.type,
                            defaultValue:obj.defaultValue,

                            srcField:""
                        })
                    }
                }

            })
        },
        onSelectChange(value) {
            this.$forceUpdate();
        },
        onOk(){
            Util.closeWindow(this,'ok',this.localConfig);
        },
        onCancel(){
            Util.closeWindow(this,'cancel');
        }
    }
}
</script>
<style scoped>

</style>

